package com.bs.blogspace.service.impl;


import com.bs.blogspace.mapper.CommentMapper;
import com.bs.blogspace.mapper.LikeMapper;
import com.bs.blogspace.mapper.SpaceArticleMapper;
import com.bs.blogspace.model.Result;
import com.bs.blogspace.pojo.Comment;
import com.bs.blogspace.pojo.Like;
import com.bs.blogspace.pojo.SpaceArticle;
import com.bs.blogspace.service.SpaceArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpaceArticleServiceImpl implements SpaceArticleService {
    @Autowired
    private SpaceArticleMapper spaceArticleMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Result addArticle(SpaceArticle spaceArticle) {
        Result result = new Result();
        List<String> list = spaceArticle.getImgsList();
        String newImg = "";
        for (int i = 0; i < list.size(); i++){
             String img = list.get(i);
             newImg = img + "@" + newImg;
        }
        spaceArticle.setImgs(newImg);
        int count = spaceArticleMapper.addArticle(spaceArticle);
        if (count == 0){
            result.setMessage("插入文章失败");
            result.setCode("414");
        }
        result.setSuccess(true);
        result.setMessage("插入文章成功");
        result.setCode("200");
        return result;
    }

    @Override
    public Result deleteArticle(String essayId) {
        Result result = new Result();
        if (StringUtils.isEmpty(essayId)){
            result.setMessage("文章id为空");
            result.setCode("414");
        }
        SpaceArticle spaceArticle = spaceArticleMapper.showByEssayId(essayId);
        if (spaceArticle == null){
            result.setMessage("文章不存在");
            result.setCode("414");
        }

        int count = spaceArticleMapper.deleteArticle(essayId);
        if (count > 0){
            result.setMessage("删除文章成功");
            result.setCode("200");
        }else {
            result.setMessage("删除文章失败");
            result.setCode("414");
        }
        return result;

    }

    @Override
    public Result getEssay(String spaceId) {
        Result<Object> result = new Result<>();
        List<SpaceArticle> list = spaceArticleMapper.getEssay(spaceId);
        ArrayList<HashMap<Object, Object>> Maplist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SpaceArticle spaceArticle = list.get(i);
            HashMap<Object, Object> sMap = new HashMap<>();
            sMap.put("essayId",spaceArticle.getEssayId());
            sMap.put("createTime",spaceArticle.getCreateTime());
            sMap.put("userName",spaceArticle.getUserName());
            sMap.put("avatar",spaceArticle.getAvatar());
            sMap.put("phone",spaceArticle.getPhone());
            sMap.put("content",spaceArticle.getContent());
            if (spaceArticle.getImgs() == null){
                sMap.put("imgs",new ArrayList<>());
            }else {
                sMap.put("imgs",spaceArticle.getImgs().split("@"));
            }
            Maplist.add(sMap);
        }
        HashMap<String, ArrayList<HashMap<Object, Object>>> hashMap = new HashMap<>();
        hashMap.put("essayList",Maplist);
        for (int i = 0; i < hashMap.get("essayList").size(); i++) {

            List<Like> likes = likeMapper.likeSelect(Integer.parseInt(String.valueOf(hashMap.get("essayList").get(i).get("essayId"))) );
            hashMap.get("essayList").get(i).put("LikePeople",likes);


            List<Comment> comments = commentMapper.queryAll((Integer) hashMap.get("essayList").get(i).get("essayId"));
            for (Comment comment : comments) {
                Map<String,String> map = new HashMap<>();
                map.put("type",comment.getType());
                map.put("userName",comment.getCommentUserName());
                comment.setTarget(map);
            }
            hashMap.get("essayList").get(i).put("comment",comments);
        }
        result.setData(hashMap);
        result.setMessage("获取成功");
        result.setCode("200");
        return result;
    }
}
